/**
 * 融云 Server API java 客户端
 * create by kitName
 * create datetime : 2017-03-13 
 * 
 * v2.0.1
 */
package io.rong;

import io.rong.methods.Chatroom;
import io.rong.methods.Group;
import io.rong.methods.Message;
import io.rong.methods.Push;
import io.rong.methods.SMS;
import io.rong.methods.User;
import io.rong.methods.Wordfilter;
import io.rong.models.HistoryMessageResult;

import java.io.BufferedInputStream;
import java.util.Enumeration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.financing.wap.util.HttpClientUtil;
import com.financing.wap.util.PropertiesUtils;



public class RongCloud {

	private static ConcurrentHashMap<String, RongCloud> rongCloud = new ConcurrentHashMap<String,RongCloud>();

	private static final String RY_APPKEY = PropertiesUtils.getValue("ry_appKey");
	private static final String RY_APPSECRET = PropertiesUtils.getValue("ry_appSecret");

	public User user;
	public Message message;
	public Wordfilter wordfilter;
	public Group group;
	public Chatroom chatroom;
	public Push push;
	public SMS sms;

	private RongCloud(String appKey, String appSecret) {
		user = new User(appKey, appSecret);
		message = new Message(appKey, appSecret);
		wordfilter = new Wordfilter(appKey, appSecret);
		group = new Group(appKey, appSecret);
		chatroom = new Chatroom(appKey, appSecret);
		push = new Push(appKey, appSecret);
		sms = new SMS(appKey, appSecret);

	}

	public static RongCloud getInstance(String appKey, String appSecret) {
		if (null == rongCloud.get(appKey)) {
			rongCloud.putIfAbsent(appKey, new RongCloud(appKey, appSecret));
		}
		return rongCloud.get(appKey);
	}
	public static RongCloud getInstance() {
		if (null == rongCloud.get(RY_APPKEY)) {
			rongCloud.putIfAbsent(RY_APPKEY, new RongCloud(RY_APPKEY, RY_APPSECRET));
		}
		return rongCloud.get(RY_APPKEY);
	}

	public static void main(String[] args) throws  Exception{
		// 消息历史记录下载地址获取 方法消息历史记录下载地址获取方法。获取 APP 内指定某天某小时内的所有会话消息记录的下载地址。（目前支持二人会话、讨论组、群组、聊天室、客服、系统通知消息历史记录下载）
		HistoryMessageResult messageGetHistoryResult = RongCloud.getInstance().message.getHistory("2017071710");
		System.out.println("getHistory:  " + messageGetHistoryResult.toString());
		String url = messageGetHistoryResult.getUrl();
		String fileName=url.substring(url.lastIndexOf("/")+1);
		String fullPath="D://"+fileName;
		HttpClientUtil httpClient = HttpClientUtil.getInstance();
		httpClient.doRequestStream(url, fullPath);
		ZipFile zip = new ZipFile(fullPath); 
		Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();  
		//该接口仅返回一个文本
		while(entries.hasMoreElements()){
			ZipEntry entry = entries.nextElement();  
			BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry));
			String s=IOUtils.toString(bis);
			String[] split = s.split("\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}");
			for (int i = 0; i < split.length; i++) {
				if(StringUtils.isBlank(split[i]))continue;
				//入库
				String dto = split[i].trim();
				System.out.println(dto);
			}
		}
	}
}